Ext.onReady(function() {
	Ext.BLANK_IMAGE_URL = '../ext/resources/images/default/s.gif';
	/**
	 * 菜单
	 */
	var storeMenu = new Ext.data.Store({
		baseParams : {
			trancode : 'Role'
		},
		proxy : new Ext.data.HttpProxy({
			url : '../' + WebServiceUrl
		}),
		// create reader that reads the project records
		reader : new Ext.data.JsonReader({
			root : 'root'
		}, [{
			name : 'name',
			type : 'string'
		}, {
			name : 'id',
			type : 'string'
		}])
	});
	storeMenu.load();
	var combo = new Ext.form.ComboBox({
		store : storeMenu,
		allowBlank : false,
		triggerAction : 'all',
		displayField : 'name',
		valueField : 'id',
		hiddenName : 'name',
		editable : false,
		emptyText : '请选择一个角色....'
	});

	var Tree = Ext.tree;
	//定义根节点的Loader
	var treeloader = new Tree.TreeLoader({
		dataUrl : '../' + WebServiceUrl + '?trancode=TreeCheckBox',
		baseAttrs : {
			uiProvider : Ext.ux.TreeCheckNodeUI
		}
	});
	//异步加载根节点
	var rootnode = new Tree.AsyncTreeNode({
		id : '1',
		checked : true,
		text : '目录树根节点'
	});
	var treepanel = new Tree.TreePanel({
		el : 'container', //填充区域
		rootVisible : false, //隐藏根节点
		border : false, //边框
		animate : true, //动画效果
		autoScroll : true, //自动滚动
		enableDD : false, //拖拽节点              
		containerScroll : true,
		height : Ext.get("container").getHeight(),
		bodyStyle : 'width:100%',
		title : '可授权子系统、子模块、子窗口树形图',
		checkModel : 'cascade', //对树的级联多选  
		onlyLeafCheckable : false,//对树所有结点都可选  
		tbar : [{
			text : '角色'
		}, '-', combo, {
			text : '保存',
			iconCls : 'save',
			handler : onItemClick
		}],
		loader : treeloader
	});
	//设置根节点
	treepanel.setRootNode(rootnode);
	//响应事件，传递node参数
	treepanel.on('beforeload', function(node) {
		treepanel.loader.dataUrl = '../' + WebServiceUrl
				+ '?trancode=TreeCheckBox&id=' + combo.getValue(); //定义子节点的Loader    
	});
	treepanel.render();
	rootnode.expand(false, false);

	combo.addListener('select', function() {
		var id = this.value;
		treepanel.dataUrl = '../' + WebServiceUrl
				+ '?trancode=TreeCheckBox&id=' + id;
		rootnode.loaded = false;
		rootnode.expand();
		treepanel.expandAll();
	});
	function Succ() {
		Ext.MessageBox.alert('操作成功', "权限保存成功！");
	}
	function Fail() {
		Ext.MessageBox.alert('操作失败', "权限保存失败！");
	}
	//获取选中节点
	function onItemClick(checked) {
		var strjs = combo.getValue();
		if (strjs == "") {
			Ext.MessageBox.alert('提示信息', "请先选择角色！");
			combo.focus(true, true);
			return false;
		}
		var checkedNodes = treepanel.getChecked();//tree必须事先创建好.
		var s = [];
		for (var i = 0; i < checkedNodes.length; i++) {
			s.push(checkedNodes[i].id)
		}
		Ext.Ajax.request({
			url : '../' + WebServiceUrl,
			success : Succ,
			failure : Fail,
			headers : {
				'my-header' : 'foo'
			},
			params : {
				strjs : strjs,
				arrcd : s.join(','),
				trancode : '1032'
			}
		});
	};
});
